import os
import pytest
from common.data_util import readYaml
from appium import webdriver
from common.LoggerUtil import LogUtils
from common.SmtpConfig.Send_TestHtml import Send_SmtpLib
from common.rmConfig.rm_logs import clear_file


# 判断app是否安装，参数是app的包名称
def is_app_install(driver):
    driver.is_app_installed("com.youth.startup")
    return is_app_install

# 安装app
def app_install(driver):
    driver.install_app(r'F:\TJC\TestCode\QC_APP\QC_APP_TestCode2\app\app-test.apk')
    return app_install


@pytest.fixture(scope="session", autouse=True)  # 设置为session级别  session级别的fixture在整个测试会话中只会被执行一次
def read_config_yaml():
    rootPath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    path = os.path.join(rootPath, "./config/config.yaml")
    data = readYaml(path)
    # 保留7天日志文件
    clear_file.clean_old_logs(r'F:\TJC\TestCode\QC_APP\QC_APP_TestCode2\outputs\logs', days_to_keep=7)
    # 发送指令给到appium
    try:
        driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", data["desired_caps"])
        driver.reset() # 重置应用程序状态
        LogUtils.info(f"-----读取appium配置文件：{driver}")
        driver.implicitly_wait(20)
        yield driver
        driver.quit()
        # Send_SmtpLib.smtpLib() # 发送测试日志
    except Exception as e:
        LogUtils.error(f"-----读取appium配置文件异常：{e}")


